﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace JLL.China.Web.Libraries
{
    public static class HelperUtility
    {
        public static int GetIdCountry()
        {
            //do
         #region "Get value from Webconfig"
         //
        int country = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["CountryID"].ToString());
            return country;
        }
        public static string PortalUrl()
        {
            return System.Configuration.ConfigurationManager.AppSettings["portal_url"].ToString();
        }
        //
        public static string ReturnStringByStep(string step)
        {
            return (HttpContext.Current.Request.Url.AbsolutePath.IndexOf(step) > 0) ? "step-no" : "step-no-off";
        }
        //
        public static float GetRateUSD()
        {
            float RateUSD = float.Parse(System.Configuration.ConfigurationManager.AppSettings["RateUSD"].ToString());
            return RateUSD;
        }

        #endregion
        #region "IMAGE"

        public static byte[] MakeThumbnail(byte[] myImage, int thumbWidth, int thumbHeight)
        {
            using (MemoryStream ms = new MemoryStream())
            using (Image thumbnail = Image.FromStream(new MemoryStream(myImage)).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
            {
                thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return ms.ToArray();
            }
        }

       public static void ThumbImage(Image originalImage, int maxWidthHeight,int fileWidth, int fileHeight,string thumbName )
        {
            //
            float fx = (float)fileWidth / (float)maxWidthHeight;
            float fy = (float)fileHeight / (float)maxWidthHeight;
            //
            float f = Math.Max(fx, fy);
            int widthTh = (int)Math.Round((float)fileWidth / f );
            int heightTh = (int)Math.Round((float)fileHeight /f);
            //
            if(widthTh == 0){
                widthTh = 1;
            }
            if(heightTh == 0){
                heightTh = 0;
            }
            //
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(widthTh, heightTh);
            System.Drawing.Graphics g =  System.Drawing.Graphics.FromImage(bm);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

            //Copy
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0,0,widthTh,heightTh);
            g.DrawImage(originalImage,rect,0,0,originalImage.Width,originalImage.Height,System.Drawing.GraphicsUnit.Pixel);

            // Compresion
            System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters();
            ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,90L);
            //Save the new image
            bm.Save(thumbName, bm.RawFormat);
            bm.Dispose();
            g.Dispose();

        }
       public static Bitmap GetThumbImage(Image originalImage, int maxWidthHeight, int fileWidth, int fileHeight)
       {
           //
           float fx = (float)fileWidth / (float)maxWidthHeight;
           float fy = (float)fileHeight / (float)maxWidthHeight;
           //
           float f = Math.Max(fx, fy);
           int widthTh = (int)Math.Round((float)fileWidth / f);
           int heightTh = (int)Math.Round((float)fileHeight / f);
           //
           if (widthTh == 0)
           {
               widthTh = 1;
           }
           if (heightTh == 0)
           {
               heightTh = 0;
           }
           //
           System.Drawing.Bitmap bm = new System.Drawing.Bitmap(widthTh, heightTh);
           System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
           g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
           g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
           g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

           //Copy
           System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, widthTh, heightTh);
           g.DrawImage(originalImage, rect, 0, 0, originalImage.Width, originalImage.Height, System.Drawing.GraphicsUnit.Pixel);

           // Compresion
           System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters();
           ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
           //Save the new image
          // bm.Save(thumbName, bm.RawFormat);
           return bm;
       }
        // Copy file
       public static void CopyFiles(DirectoryInfo source, DirectoryInfo target, string prodirid, string ListignID)
       {
           try
           {
               //check if the target directory exists
               if (Directory.Exists(target.FullName) == false)
               {
                   Directory.CreateDirectory(target.FullName);
               }

               //copy all the files into the new directory

               foreach (FileInfo fi in source.GetFiles())
               {

                   fi.CopyTo(Path.Combine(target.ToString(), fi.Name.Replace(prodirid, ListignID)), true);
               }


               //copy all the sub directories using recursion

               foreach (DirectoryInfo diSourceDir in source.GetDirectories())
               {
                   DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                   CopyFiles(diSourceDir, nextTargetDir, prodirid, ListignID);
               }
              
           }
           catch (IOException ie)
           {
              
           }
       }
        #endregion


       public static byte[] imageToByteArray(System.Drawing.Image imageIn)
       {
           MemoryStream ms = new MemoryStream();
           imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
           return ms.ToArray();
       }
       public static byte[] ReadFile(string sPath)
       {
           //Initialize byte array with a null value initially.
           byte[] data = null;

           //Use FileInfo object to get file size.
           try
           {
               FileInfo fInfo = new FileInfo(sPath);
               long numBytes = fInfo.Length;

               //Open FileStream to read file
               FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

               //Use BinaryReader to read file stream into byte array.
               BinaryReader br = new BinaryReader(fStream);

               //When you use BinaryReader, you need to supply number of bytes to read from file.
               //In this case we want to read entire file. So supplying total number of bytes.
               data = br.ReadBytes((int)numBytes);
               return data;
           }
           catch (Exception)
           {
               return data;
               
           }
               
           

           
       }
       public static Image byteArrayToImage(byte[] byteArray)
       {
           MemoryStream ms = new MemoryStream();

           ms = new MemoryStream(byteArray, 0, byteArray.Length);
           ms.Seek(0, SeekOrigin.Begin);
           System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
           return returnImage;
       }

       private static Image CreateReducedImage(Image imgOrig, Size newSize)
       {
           var newBm = new Bitmap(newSize.Width, newSize.Height);
           using (var newGrapics = Graphics.FromImage(newBm))
           {
               newGrapics.CompositingQuality = CompositingQuality.HighSpeed;
               newGrapics.SmoothingMode = SmoothingMode.HighSpeed;
               newGrapics.InterpolationMode = InterpolationMode.HighQualityBicubic;
               newGrapics.DrawImage(imgOrig, new Rectangle(0, 0, newSize.Width, newSize.Height));
           }

           return newBm;
       }

    }
}
